﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Domain.Values
{
	using System.Collections.Generic;
	using System.Web.Mvc;
	using Falcon.Core;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Forums.Domain.Enums;
	using Falcon.Forums.Domain.Services;

	public class ForumPostCopy : ValueBase<ForumPostCopy>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ForumPostCopy"/> class.
		/// </summary>
		public ForumPostCopy()
		{
			this.ForumService = Application.Resolve<IForumService>();
			this.ForumTopicService = Application.Resolve<IForumTopicService>();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ForumPostCopy"/> class.
		/// </summary>
		/// <param name="posts">The posts.</param>
		public ForumPostCopy(IList<ForumPost> posts) : this()
		{
			this.Posts = posts;
		}

		/// <summary>
		/// Gets or sets the forum repository.
		/// </summary>
		/// <value>The forum repository.</value>
		public IForumService ForumService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the forum topic service.
		/// </summary>
		/// <value>The forum topic service.</value>
		public IForumTopicService ForumTopicService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the forum id.
		/// </summary>
		/// <value>The forum id.</value>
		public int ForumId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the topic id.
		/// </summary>
		/// <value>The topic id.</value>
		public int TopicId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the redirect.
		/// </summary>
		/// <value>The redirect.</value>
		public string Redirect
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the action.
		/// </summary>
		/// <value>The action.</value>
		public ForumPostCopyAction Action
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the posts.
		/// </summary>
		/// <value>The posts.</value>
		public IList<ForumPost> Posts
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the posts to move.
		/// </summary>
		/// <value>The posts to move.</value>
		public ForumPostUpdateValues[] PostsToCopy
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the parents.
		/// </summary>
		/// <value>The parents.</value>
		public SelectList Forums
		{
			get
			{
				return new SelectList(ForumService.GetAll(), "Id", "Title", this.ForumId);
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<ForumPostCopy> Rules()
		{
			var rules = base.Rules();

			if (this.Action == ForumPostCopyAction.Create)
			{
				rules.Add(x => x.Title)
					.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));
			}
			else
			{
				var topic = this.ForumTopicService.GetById(this.TopicId);

				if (topic == null)
				{
					rules.Result.Add(new ValidationError
					{
						Bind = "TopicId",
						Message = ForumsResources.Errors.ForumTopicNotFound
					});
				}
			}

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}